package com.example.managemyself.data.model;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.Ignore;
import androidx.room.PrimaryKey;
import androidx.room.TypeConverters;

import com.example.managemyself.util.DateConverter;

import java.util.Date;

/**
 * 用户数据模型
 */
@Entity(tableName = "users")
@TypeConverters(DateConverter.class)
public class User {
    
    @PrimaryKey(autoGenerate = true)
    private int id;
    
    @NonNull
    private String username;
    
    @NonNull
    private String email;
    
    private String password; // 实际项目中应该存储加密后的密码
    
    private String nickname;
    
    private String avatar; // 头像URL或路径
    
    @NonNull
    private String role; // 用户角色：USER, ADMIN
    
    private Date createdDate;
    
    private Date lastLoginDate;
    
    private boolean isActive; // 账户是否激活
    
    // 扩展的个人资料字段
    private String phone;           // 手机号
    private Date birthday;          // 生日
    private String gender;          // 性别：male, female
    private String bio;             // 个人简介
    private String constellation;   // 星座
    private String hobbies;         // 爱好
    private String occupation;      // 职业
    private String city;            // 城市
    private boolean publicBirthday; // 是否公开生日
    private boolean publicContact;  // 是否公开联系方式
    
    // 用户角色常量
    public static final String ROLE_USER = "USER";
    public static final String ROLE_ADMIN = "ADMIN";
    
    // 默认构造函数（Room使用）
    public User() {
        this.role = ROLE_USER; // 默认为普通用户
        this.createdDate = new Date();
        this.isActive = true;
    }
    
    // 带参数构造函数（应用程序使用，Room忽略）
    @Ignore
    public User(@NonNull String username, @NonNull String email, String password) {
        this();
        this.username = username;
        this.email = email;
        this.password = password;
    }
    
    // Getter和Setter方法
    public int getId() {
        return id;
    }
    
    public void setId(int id) {
        this.id = id;
    }
    
    @NonNull
    public String getUsername() {
        return username;
    }
    
    public void setUsername(@NonNull String username) {
        this.username = username;
    }
    
    @NonNull
    public String getEmail() {
        return email;
    }
    
    public void setEmail(@NonNull String email) {
        this.email = email;
    }
    
    public String getPassword() {
        return password;
    }
    
    public void setPassword(String password) {
        this.password = password;
    }
    
    public String getNickname() {
        return nickname;
    }
    
    public void setNickname(String nickname) {
        this.nickname = nickname;
    }
    
    public String getAvatar() {
        return avatar;
    }
    
    public void setAvatar(String avatar) {
        this.avatar = avatar;
    }
    
    @NonNull
    public String getRole() {
        return role;
    }
    
    public void setRole(@NonNull String role) {
        this.role = role;
    }
    
    public Date getCreatedDate() {
        return createdDate;
    }
    
    public void setCreatedDate(Date createdDate) {
        this.createdDate = createdDate;
    }
    
    public Date getLastLoginDate() {
        return lastLoginDate;
    }
    
    public void setLastLoginDate(Date lastLoginDate) {
        this.lastLoginDate = lastLoginDate;
    }
    
    public boolean isActive() {
        return isActive;
    }
    
    public void setActive(boolean active) {
        isActive = active;
    }
    
    // 便利方法
    /**
     * 判断是否为管理员
     */
    public boolean isAdmin() {
        return ROLE_ADMIN.equals(this.role);
    }
    
    /**
     * 判断是否为普通用户
     */
    public boolean isUser() {
        return ROLE_USER.equals(this.role);
    }
    
    /**
     * 获取显示名称（优先显示昵称，否则显示用户名）
     */
    public String getDisplayName() {
        return (nickname != null && !nickname.trim().isEmpty()) ? nickname : username;
    }
    
    // 扩展字段的getter和setter方法
    public String getPhone() {
        return phone;
    }
    
    public void setPhone(String phone) {
        this.phone = phone;
    }
    
    public Date getBirthday() {
        return birthday;
    }
    
    public void setBirthday(Date birthday) {
        this.birthday = birthday;
    }
    
    public String getGender() {
        return gender;
    }
    
    public void setGender(String gender) {
        this.gender = gender;
    }
    
    public String getBio() {
        return bio;
    }
    
    public void setBio(String bio) {
        this.bio = bio;
    }
    
    public String getConstellation() {
        return constellation;
    }
    
    public void setConstellation(String constellation) {
        this.constellation = constellation;
    }
    
    public String getHobbies() {
        return hobbies;
    }
    
    public void setHobbies(String hobbies) {
        this.hobbies = hobbies;
    }
    
    public String getOccupation() {
        return occupation;
    }
    
    public void setOccupation(String occupation) {
        this.occupation = occupation;
    }
    
    public String getCity() {
        return city;
    }
    
    public void setCity(String city) {
        this.city = city;
    }
    
    public boolean isPublicBirthday() {
        return publicBirthday;
    }
    
    public void setPublicBirthday(boolean publicBirthday) {
        this.publicBirthday = publicBirthday;
    }
    
    public boolean isPublicContact() {
        return publicContact;
    }
    
    public void setPublicContact(boolean publicContact) {
        this.publicContact = publicContact;
    }
}
